package engine.scene
{
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class TimerEventLogic
	{
		private var _tTimer			:Timer;
		
		private var _callbackFunc	:Function;
		
		private var _endbackFunc	:Function;
		
		private var _pList			:Array;
		
		private var _isImmediately	:Boolean;
		
		private var _isReady		:Boolean	=	false;
		
		private static var _logics	:Array		=	new Array();
		
		public function get isReady():Boolean
		{
			return _isReady;
		}
		
		public function TimerEventLogic()
		{
			_isReady	=	true;
		}
		
		/**
		 * 
		 * @param delay			事件总持续时间
		 * @param repeatCount	次数
		 * @param callbackFunc	回调函数
		 * @param endbackFunc	事件完整完成后的回调函数
		 * @param isImmediately	是否立即刷新渲染
		 * @param pList			随回调的函数
		 * 
		 */
		public static function StartTimerEvent(
			delay			:uint,
			repeatCount		:uint,
			callbackFunc	:Function,
			endbackFunc		:Function,
			isImmediately	:Boolean	= false,
			pList			:Array 		= null):void
		{
			var tLogic:TimerEventLogic;
			if(_logics.length == 0)
			{
				tLogic	=	new TimerEventLogic();
				_logics.push(tLogic);
			}
			else
			{
				var tSize:int	=	_logics.length;
				
				for(var i:int = 0 ; i < tSize ; i++)
				{
					tLogic	=	_logics[i];
					if(tLogic.isReady)
					{
						tLogic.start(delay,repeatCount,callbackFunc,endbackFunc,isImmediately,pList);
						return;
					}
				}
				
				tLogic	=	new TimerEventLogic();
				_logics.push(tLogic);
			}
		}
		
		/**
		 * 
		 * @param delay			事件总持续时间
		 * @param repeatCount	次数
		 * @param callbackFunc	回调函数
		 * @param isImmediately	是否立即刷新渲染
		 * @param pList			随回调的函数
		 * 
		 */
		public function start(
			delay			:uint,
			repeatCount		:uint,
			callbackFunc	:Function,
			endbackFunc		:Function,
			isImmediately	:Boolean	= false,
			pList			:Array 		= null):void
		{
			if(_isReady)
				_isReady	=	false;
			
			if(_tTimer == null)
				_tTimer	=	new Timer(delay/repeatCount,repeatCount);
			
			reSet();
			
			_isImmediately			=	isImmediately
			_pList					=	pList;
			_callbackFunc			=	callbackFunc
			_endbackFunc			=	endbackFunc;
			
			_tTimer.delay			=	delay;
			_tTimer.repeatCount		=	repeatCount;
			
			_tTimer.addEventListener(TimerEvent.TIMER,onRunning);
			_tTimer.addEventListener(TimerEvent.TIMER_COMPLETE,onOver);
			_tTimer.start();
		}
		
		private function onRunning(e:TimerEvent):void
		{
			_callbackFunc.call(null,_pList);
			if(_isImmediately)
				e.updateAfterEvent();
		}
		
		private function reSet():void
		{
			_tTimer.stop();
			_tTimer.reset();
			_tTimer.removeEventListener(TimerEvent.TIMER,onRunning);
			_tTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,onOver);
			_isImmediately			=	false;
			_pList					=	null;
			_callbackFunc			=	null;
			_endbackFunc			=	null;
			_isReady				=	true;
		}
		
		private function onOver(e:TimerEvent):void
		{
			if(_endbackFunc!=null)
				_endbackFunc.call(null,_pList);
			reSet();
		}
	}
}